/**
 * @Author:  renyc
 * @Date: 2022年12月8日14:56:41
 */

'use strict';

import * as Cesium from "cesium/Cesium";
import Animation from "./Animation";
import OverlayType from "../OverlayType";
import Transformer from "../../utils/Transformer";
import Util from "../../utils/Util";
const RadarScanShader = require('./shader/radar/RadarScanShader.glsl');
 
class RadarScanner extends Animation{
     constructor(position, radius, options = {}){
        super();

        this._viewer = undefined;
        this._position = position;
        this._radius = radius || 100;
        this._color = options.color || Cesium.Color.GREEN;
        this._speed = options.speed || 3
        this._delegate = undefined;
        this._options = options;

        this._type = OverlayType.RADAR;
     }
 
     /**
      * 将Layer挂载到Viewer上(内部方法)
      * @private
      * @Override
      */
    _attach(){
        this._viewer = this._layer._viewer;
        return true;
      }
 
    /**
       * 将Layer从Viewer上卸载(内部方法)
       * @private
       * @Override
       */
    _detach(){
        this.stop();
        this._viewer = undefined;
      }

      /**
     *
     * @private
     */
    _mountContent() {
        let center = Transformer.transformWGS84ToCartesian(this._position);
        let up = Cesium.Ellipsoid.WGS84.geodeticSurfaceNormal(
            center,
            new Cesium.Cartesian3()
        );
        let time = new Date().getTime();
        let self = this;
        this._delegate = new Cesium.PostProcessStage({
            name: Util.uuid(),
            fragmentShader: RadarScanShader,
            uniforms: {
                centerWC: function() {
                    return center
                },
                planeNormalWC: function() {
                    return up
                },
                lineNormalWC: function() {
                    let rotateQ = new Cesium.Quaternion();
                    let rotateM = new Cesium.Matrix3();
                    let east = Cesium.Cartesian3.cross(
                        Cesium.Cartesian3.UNIT_Z,
                        up,
                        new Cesium.Cartesian3()
                    )
                    let now = new Date().getTime();
                    let angle = Cesium.Math.PI * 2 * ((now - time) / 1e4) * self._speed;
                    Cesium.Quaternion.fromAxisAngle(up, angle, rotateQ);
                    Cesium.Matrix3.fromQuaternion(rotateQ, rotateM);
                    Cesium.Matrix3.multiplyByVector(rotateM, east, east);
                    Cesium.Cartesian3.normalize(east, east);
                    return east;
                },
                radius: function() {
                    return self._radius;
                },
                color: function() {
                    return self._color;
                }
            }
        })
    }

    /**
     *
     * @returns {RadarScan}
     */
    start() {
        !this._delegate && this._mountContent();
        this._delegate && this._viewer.scene.postProcessStages.add(this._delegate);
        return this;
    }

    /**
     *
     * @returns {RadarScan}
     */
    stop() {
        this._delegate &&
            this._viewer.scene.postProcessStages.remove(this._delegate);
        this._delegate = undefined;
        return this;
    }
 }
 
 export default RadarScanner;
 